// pages/my/my.ts
import {staffCheck,myReport} from "../../../http/api";
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid:"",
    phone:"",
    id:"",
    userType:"",
    staffId:"",


    userId:"",
    amountOrder:"",
    amountPrice:"",
    monthOrder:"",
    oneOrder:"",
   
  },
  showBuilding(){
    wx.showToast({
      title: '建设中...',
      icon:'none'
    })
  },

  toPage(e){
    
    
    const {url} = e.currentTarget.dataset  
    
    if(url === '/pages/shop/index/index'){
      app.shopSubscribeMessageNewOrder()
    }
    wx.navigateTo({
      url
    })
  },
  toOrder(e){
    const {type,url} = e.currentTarget.dataset
    wx.setStorageSync('oType', type)
    wx.switchTab({
      url:url,
    })

  },


  getUserType(){
    wx.showLoading()
    const phone = wx.getStorageSync('phone')
    staffCheck({phone}).then(res=>{
      if(res.data.code === 200){
        const {staffType,id} = res.data.data
        this.setData({
          userType:staffType,
          staffId:id
        })
        wx.setStorageSync('userType', staffType);
        wx.setStorageSync('staffId', id)
       
      }
    }).finally(()=>{
      wx.hideLoading()
    })
  },

  getOrderReport(){
    wx.showLoading()
    const customerId = wx.getStorageSync('id')
    myReport({customerId}).then(res=>{
      if(res.data.code === 200){
        const {amountOrder,amountPrice,monthOrder,oneOrder} = res.data.data
        this.setData({
          amountOrder,amountPrice,monthOrder,oneOrder
        })
        
       
      }
    }).finally(()=>{
      wx.hideLoading()
    })
  },


  init(){
    const openid = wx.getStorageSync('openid')
    const phone = wx.getStorageSync('openid')
    if(openid){
      let userId = openid.slice(0,7)
      this.setData({
        userId:`SGT_${userId}`
      })
    }
    this.getOrderReport()

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    app.checkIsLogin()
    this.setData({
      phone:wx.getStorageSync('phone'),
      id:wx.getStorageSync('id'),
      openid:wx.getStorageSync('openid'),
    })
    this.init()

    if(this.data.userType===""){
      this.getUserType()
    }

    

    




  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})